package com.youdianda.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.youdianda.dto.ListAddDataDTO;
import com.youdianda.dto.YddAdvimgDTO;
import com.youdianda.entity.YddAdvimg;
import com.youdianda.entity.YddAdvpos;

import com.youdianda.mapper.YddAdvimgMapper;
import com.youdianda.result.PageResult;
import com.youdianda.result.Result;
import com.youdianda.service.IYddAdvimgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.service.IYddAdvposService;
import com.youdianda.vo.FListDataVO;
import com.youdianda.vo.ListDataVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg> implements IYddAdvimgService {

    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;

    @Autowired
    private IYddAdvposService iYddAdvposService;

    /**
     * 查询
     * @param id
     * @return
     */
    @Override
    public Result Selectpos(Integer id) {

        YddAdvimg yddAdvimg= yddAdvimgMapper.selectById(id);
        return Result.success(yddAdvimg);
    }

    @Override
    public Result listYddAdvimgVO(YddAdvimgDTO dto) {
        Page<YddAdvimg> page =new Page<>(Long.parseLong(dto.getPage()),Long.parseLong(dto.getLimit()));


        Page<YddAdvimg> page1 = page(page, Wrappers.<YddAdvimg>lambdaQuery().
                like(dto.getAdvimgdesc()!=null && dto.getAdvimgdesc()!="",YddAdvimg::getAdvimgdesc, dto.getAdvimgdesc())
                .eq(dto.getAdvimgpos()!=null && dto.getAdvimgpos()!="",YddAdvimg::getAdvimgpos, dto.getAdvimgpos()));

//        LambdaQueryWrapper<YddAdvimg> lqwYddAdvimg=new LambdaQueryWrapper<>();
//        if (dto.getAdvimgdesc()!=null && dto.getAdvimgdesc()!=""){
//            lqwYddAdvimg.like(YddAdvimg::getAdvimgdesc,dto.getAdvimgdesc());
//        }
//        if (dto.getAdvimgpos()!=null && dto.getAdvimgpos()!=""){
//
//            lqwYddAdvimg.eq(YddAdvimg::getId,dto.getAdvimgpos());
//        }
//
        LambdaQueryWrapper<YddAdvpos> lqwYddAdvpos = new LambdaQueryWrapper();
//        Page<YddAdvimg> page1 = page(page,lqwYddAdvimg);


        //创建一个list集合封装ListDataVO
        List<FListDataVO> data=new ArrayList<>();
        //遍历
        for (YddAdvimg yddAdvimg : page1.getRecords()) {
            FListDataVO listDataVO = new FListDataVO();
            BeanUtils.copyProperties(yddAdvimg,listDataVO);
            //关联两个表id字段
            lqwYddAdvpos.eq(YddAdvpos::getId,yddAdvimg.getAdvimgpos());
            YddAdvpos yddAdvpos = iYddAdvposService.getOne(lqwYddAdvpos);
            if (yddAdvpos==null){
                break;
            }
            //得到另一个表的字段

            String advposname = yddAdvpos.getAdvposname();

            listDataVO.setAdvposname(advposname);
            data.add(listDataVO);
            lqwYddAdvpos.clear();
        }

        PageResult pageResult = new PageResult();
        pageResult.setData(data);
        pageResult.setCurrentPage(1);
        pageResult.setCount(5);
        pageResult.setPageSize(2);
        pageResult.setTotal(1);
        return Result.success(pageResult);


    }

    /**
     * 修改
     * @param yddAdvimg
     * @return
     */
    @Override
    public Result update(YddAdvimg yddAdvimg) {

        int update = yddAdvimgMapper.updateById(yddAdvimg);

        return Result.success(update);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Result delete(Integer id) {
       yddAdvimgMapper.deleteById(id);
        return Result.success(id);
    }

    @Override
    public Result getyddvimadd(ListAddDataDTO listAddDataDTO) {

        YddAdvimg yddAdvimg = new YddAdvimg();
        BeanUtils.copyProperties(listAddDataDTO,yddAdvimg);
        save(yddAdvimg);

        return Result.success();


    }


}
